import { goodsForHome } from "../../../../data/goods";
Page({
  data: {
    isLoading:true,
    search_showed:true,
    kw:'',
    currentNav:0,
    priceType:'',
    filter_showed:false,
    price_low:'',
    price_high:'',
    goods: [],
  },
  onLoad(option) {
    this.setData({kw:option.kw});
    this.setData({search_showed:parseInt(option.search)!=-1});
    //
    this.setData({goods:goodsForHome()});
    setTimeout(()=>{
      this.setData({isLoading:false});
    },1000);
  },
  search(e) {
    console.log('search:',e.detail);
  },
  selectNav(e) {
    let index=e.currentTarget.dataset.i;
    if(parseInt(index)!=2){
      this.setData({isLoading:true});
    }
    if(index==1){
      if(index==this.data.currentNav){
        if(this.data.priceType=='up'){
          this.setData({priceType:'down'});
          this.setData({goods:this.data.goods.sort((a,b)=>{
            return a.price>b.price?1:-1
          })});
        }else{
          this.setData({priceType:'up'});
          this.setData({goods:this.data.goods.sort((a,b)=>{
            return a.price<b.price?1:-1
          })});
        }
      }else{
        this.setData({priceType:'down'});
        this.setData({goods:this.data.goods.sort((a,b)=>{
          return a.price>b.price?1:-1
        })});
      }
    }else{
      this.setData({priceType:''});
    }
    if(index==0){
      this.setData({goods:goodsForHome()});
    }
    this.setData({currentNav:index});
    //console.log('index:',index);
    if(index==2){
      this.setData({filter_showed:true});
    }else{
      setTimeout(()=>{
        this.setData({isLoading:false});
      },600);
    }
  },
  onFilterClose(e) {
    this.setData({filter_showed:false});
  },
  onFilterInputChange(e) {
    //console.log('onFilterInputChange:',e);
    if(e.currentTarget.id=="filter_input1"){
      this.setData({price_low:e.detail});
    }
    if(e.currentTarget.id=="filter_input2"){
      this.setData({price_high:e.detail});
    }
  },
  resetFilter(e) {
    this.setData({price_low:''});
    this.setData({price_high:''});
  },
  confirmFilter(e) {
    if(this.data.price_low==''|| this.data.price_high==''){
      wx.showToast({
        title: '请填写价格范围',
      });
      return;
    }
    /*
    wx.showToast({
      title: this.data.price_low+'~'+this.data.price_high,
    });
    */
    this.setData({filter_showed:false});
    //
    let low =parseFloat(this.data.price_low);
    let high=parseFloat(this.data.price_high);
    let goods=this.data.goods;
    goods=goods.filter(item=>{
      return item.price>=low && item.price<high;
    });
    goods=goods.filter(item=>{
      return item.price>=low && item.price<high;
    });
    goods=goods.sort((a,b)=>{
      return a.price>b.price?1:-1
    });
    this.setData({isLoading:true});
    this.setData({goods:goods});
    setTimeout(()=>{
      this.setData({isLoading:false});
    },600);
  }
})